package com.gxa.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.comment.mapper.CommentMapper;
import com.gxa.comment.service.CommentService;
import com.gxa.common.R;
import com.gxa.common.entity.product_review.CommentTable;
import com.gxa.common.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentTable> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public R findAllComment(Integer currentPage, Integer pageSize, Long orderId, String byCommentMan, String commentMan, String contentKeyword, Byte commentTime, Byte reviewResult, String reviewer) {

        Timestamp currentTime = null;
        Timestamp startTime = null;

        if (commentTime != null) {
            // 获取当前时间
            currentTime = new Timestamp(System.currentTimeMillis());
            switch (commentTime) {
                // 选择近三个月
                case 1:
                    startTime = MyUtils.get3MonthBefore();
                    break;
                case 2:
                    startTime = MyUtils.getYearTimestamp("2022");
                    break;
                case 3:
                    startTime = MyUtils.getYearTimestamp("2021");
                    break;
                case 4:
                    startTime = MyUtils.getYearTimestamp("2020");
                    break;
                case 5:
                    startTime = MyUtils.getYearTimestamp("2020");
                    break;
            }

        }
        //构造QueryWrapper进行筛选
        QueryWrapper<CommentTable> queryWrapper = new QueryWrapper();
        queryWrapper.eq(orderId != null, "order_id", orderId)
                .and(byCommentMan != null, Query -> Query
                        .eq("deleveryman_name", byCommentMan)
                        .or().eq("shop_chinese_name", byCommentMan))
                .and(commentMan != null, Query -> Query
                        .eq("customer_name", commentMan)
                        .or().eq("shop_chinese_name", commentMan))
                .like(contentKeyword != null, "content", contentKeyword)
                .between(commentTime != null && commentTime > 0 && commentTime < 5, "comment_time", startTime, currentTime)
                .le(commentTime != null && commentTime == 5, "comment_time", startTime);
        //分页查询
        Page<CommentTable> page = new Page<>(currentPage, pageSize);
        Page<CommentTable> selectPage = commentMapper.selectPage(page, queryWrapper);
        //获取查询到的评论列表对象
        List<CommentTable> commentList = selectPage.getRecords();

        R r = new R();
        r.setCode(200);
        r.setMessage("success");
        r.setData(commentList);
        System.out.println(r.getData());

        return r;
    }

    @Override
    public R findAllCommentById(Long commentId) {
        CommentTable comment = commentMapper.selectById(commentId);
        R r = new R();
        r.setCode(200);
        r.setMessage("success");
        r.setData(comment);
        return r;
    }

    @Override
    public R updateComment(CommentTable commentTable) {

        int i = commentMapper.updateById(commentTable);

        R r = new R();
        r.setCode(200);
        r.setMessage("修改成功");
        return r;
    }
}
